package com.lys.service.impl;

import com.lys.service.WeatherDataCollectionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class IWeatherDataCollectionService implements WeatherDataCollectionService {
    private final static Logger logger= LoggerFactory.getLogger(IWeatherDataCollectionService.class);

    private static final String WEATHER_URI = "http://wthrcdn.etouch.cn/weather_mini?";


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void syncDataByCityId(String cityId) {
        saveWeatherData(cityId);
    }

    /**
     * 存放天气数据缓存
     * @param key
     */
    private void saveWeatherData(String key){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String strBody = null;
        String uri = WEATHER_URI+"citykey="+key;
        ResponseEntity<String> respString = restTemplate.getForEntity(uri, String.class);
        if (respString.getStatusCodeValue() == 200) {
            strBody = respString.getBody();
        }
        ops.set(key,strBody);
    }
}
